package com.agile.major.web.rest;

import com.agile.major.domain.FreshGraduateBookAndTrain;
import com.agile.major.service.FreshGraduateBookAndTrainService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing FreshGraduateBookAndTrain.
 */
@RestController
@RequestMapping("/api")
public class FreshGraduateBookAndTrainResource {

    private final Logger log = LoggerFactory.getLogger(FreshGraduateBookAndTrainResource.class);

    private static final String ENTITY_NAME = "freshGraduateBookAndTrain";

    private final FreshGraduateBookAndTrainService freshGraduateBookAndTrainService;

    public FreshGraduateBookAndTrainResource(FreshGraduateBookAndTrainService freshGraduateBookAndTrainService) {
        this.freshGraduateBookAndTrainService = freshGraduateBookAndTrainService;
    }

    /**
     * POST  /fresh-graduate-book-and-trains : Create a new freshGraduateBookAndTrain.
     *
     * @param freshGraduateBookAndTrain the freshGraduateBookAndTrain to create
     * @return the ResponseEntity with status 201 (Created) and with body the new freshGraduateBookAndTrain, or with status 400 (Bad Request) if the freshGraduateBookAndTrain has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/fresh-graduate-book-and-trains")
    @Timed
    public ResponseEntity<FreshGraduateBookAndTrain> createFreshGraduateBookAndTrain(@RequestBody FreshGraduateBookAndTrain freshGraduateBookAndTrain) throws URISyntaxException {
        log.debug("REST request to save FreshGraduateBookAndTrain : {}", freshGraduateBookAndTrain);
        if (freshGraduateBookAndTrain.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new freshGraduateBookAndTrain cannot already have an ID")).body(null);
        }
        FreshGraduateBookAndTrain result = freshGraduateBookAndTrainService.save(freshGraduateBookAndTrain);
        return ResponseEntity.created(new URI("/api/fresh-graduate-book-and-trains/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /fresh-graduate-book-and-trains : Updates an existing freshGraduateBookAndTrain.
     *
     * @param freshGraduateBookAndTrain the freshGraduateBookAndTrain to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated freshGraduateBookAndTrain,
     * or with status 400 (Bad Request) if the freshGraduateBookAndTrain is not valid,
     * or with status 500 (Internal Server Error) if the freshGraduateBookAndTrain couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/fresh-graduate-book-and-trains")
    @Timed
    public ResponseEntity<FreshGraduateBookAndTrain> updateFreshGraduateBookAndTrain(@RequestBody FreshGraduateBookAndTrain freshGraduateBookAndTrain) throws URISyntaxException {
        log.debug("REST request to update FreshGraduateBookAndTrain : {}", freshGraduateBookAndTrain);
        if (freshGraduateBookAndTrain.getId() == null) {
            return createFreshGraduateBookAndTrain(freshGraduateBookAndTrain);
        }
        FreshGraduateBookAndTrain result = freshGraduateBookAndTrainService.save(freshGraduateBookAndTrain);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, freshGraduateBookAndTrain.getId().toString()))
            .body(result);
    }

    /**
     * GET  /fresh-graduate-book-and-trains : get all the freshGraduateBookAndTrains.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of freshGraduateBookAndTrains in body
     */
    @GetMapping("/fresh-graduate-book-and-trains")
    @Timed
    public ResponseEntity<List<FreshGraduateBookAndTrain>> getAllFreshGraduateBookAndTrains(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of FreshGraduateBookAndTrains");
        Page<FreshGraduateBookAndTrain> page = freshGraduateBookAndTrainService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/fresh-graduate-book-and-trains");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /fresh-graduate-book-and-trains/:id : get the "id" freshGraduateBookAndTrain.
     *
     * @param id the id of the freshGraduateBookAndTrain to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the freshGraduateBookAndTrain, or with status 404 (Not Found)
     */
    @GetMapping("/fresh-graduate-book-and-trains/{id}")
    @Timed
    public ResponseEntity<FreshGraduateBookAndTrain> getFreshGraduateBookAndTrain(@PathVariable Long id) {
        log.debug("REST request to get FreshGraduateBookAndTrain : {}", id);
        FreshGraduateBookAndTrain freshGraduateBookAndTrain = freshGraduateBookAndTrainService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(freshGraduateBookAndTrain));
    }

    /**
     * DELETE  /fresh-graduate-book-and-trains/:id : delete the "id" freshGraduateBookAndTrain.
     *
     * @param id the id of the freshGraduateBookAndTrain to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/fresh-graduate-book-and-trains/{id}")
    @Timed
    public ResponseEntity<Void> deleteFreshGraduateBookAndTrain(@PathVariable Long id) {
        log.debug("REST request to delete FreshGraduateBookAndTrain : {}", id);
        freshGraduateBookAndTrainService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @GetMapping("/fresh-graduate-book-and-trains/byMajor/{id}")
    @Timed
    public ResponseEntity<Page<FreshGraduateBookAndTrain>> getFreshGraduateBookAndTrainByMajor(@PathVariable Long id,@ApiParam Pageable pageable) {
        log.debug("REST request to get MajorQcbook : {}", id);
        Page<FreshGraduateBookAndTrain> page = freshGraduateBookAndTrainService.getFreshGraduateBookAndTrainByMajor(id,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/major-qcbooks");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }
}
